package manager.connection;

import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache;

import advice.ExceptionManager;
import manager.connection.state.GMOrderStatus;
import manager.connection.type.GMOrderInfo;
import manager.pay.GetPayResult;
import net.socket.server.SocketServerInboundHandler;
import net.socket.server.protocal.PROTOID;
import net.socket.server.request.GMOrderRequest;
import net.socket.server.request.IRequest;
import net.socket.server.response.GMOrderResponse;
import observable.subject.MessageCenter;
import redis.clients.jedis.Client;
import util.MyFactory;
import util.ResultTypeList;


public class GMPlayerManager {
	private static ArrayList<String> gmList=new ArrayList<String>();
	private ClientConnectionManager ccm = null;
	private static Logger logger = Logger.getLogger(ExceptionManager.class);
	
	public void init() {
		MessageCenter mc = MyFactory.context.getBean("MessageCenter", MessageCenter.class);
		mc.register(PROTOID.REQ_GM_ORDER, (int cmd, IRequest request)->handleGMOrder(cmd, request));
		
		ccm = MyFactory.context.getBean("ClientConnectionManager", ClientConnectionManager.class);
	}
	private int handleGMOrder(int cmd, IRequest request) throws IllegalArgumentException, IllegalAccessException, JsonProcessingException, BeansException, SQLException {
		GMOrderRequest req = (GMOrderRequest)request;
		GMOrderResponse rsp = new GMOrderResponse();
		String openId =ccm.getOpenIdByMachine(req.getMachineId());
		if(req.getMachineId()==-1||"".equals(req.getOrder())||req.getOrder().length()>100) {
			rsp.setResult(ResultTypeList.NULL_ARGS);
			ccm.sendResponse(PROTOID.RSP_GM_ORDER, req.getMachineId(), rsp);
			return ResultTypeList.NULL_ARGS;
		}
		if(openId==null) {
			rsp.setResult(ResultTypeList.PLAYER_NO_LOGIN);
			ccm.sendResponse(PROTOID.RSP_GM_ORDER, req.getMachineId(), rsp);
			return ResultTypeList.PLAYER_NO_LOGIN;
		}
		if(!IsGM(openId)) {
			rsp.setResult(ResultTypeList.NOT_GM_PLAYER);
			ccm.sendResponse(PROTOID.RSP_GM_ORDER, req.getMachineId(), rsp);
			return ResultTypeList.NOT_GM_PLAYER;
		}
		//System.out.println("GM ORDER:"+req.getOrder());
		logger.info("GM ORDER:"+req.getOrder());
		GMOrderInfo gOrderInfo =translateOrder(req);
		if(gOrderInfo==null) {
			rsp.setResult(ResultTypeList.ERROR_ARGS);
			ccm.sendResponse(PROTOID.RSP_GM_ORDER, req.getMachineId(), rsp);
			return ResultTypeList.ERROR_ARGS;
		}
		switch (gOrderInfo.getType()) {
			case GMOrderStatus.KICK_MACHINE:
				MyFactory.context.getBean("SocketServerInboundHandler",SocketServerInboundHandler.class)
				.kickMachine(gOrderInfo.getMid());
				break;
			case GMOrderStatus.KICK_PLAYER:
				MyFactory.context.getBean("ClientConnectionManager",ClientConnectionManager.class)
				.kickPlayer(gOrderInfo.getMid());
				break;
			case GMOrderStatus.SET_VIP:
				MyFactory.context.getBean("GetPayResult",GetPayResult.class).setVip(gOrderInfo);
				break;
			case GMOrderStatus.GET_COIN:
				MyFactory.context.getBean("GetPayResult",GetPayResult.class).getCion(gOrderInfo);
				break;
			case GMOrderStatus.GET_TIME:
				MyFactory.context.getBean("GetPayResult",GetPayResult.class).getTime(gOrderInfo);
				break;
			default:
				break;
		}
		rsp.setResult(ResultTypeList.SUCCESS);
		ccm.sendResponse(PROTOID.RSP_GM_ORDER, req.getMachineId(), rsp);
		return ResultTypeList.SUCCESS;
	}
	
	private GMOrderInfo translateOrder(GMOrderRequest req) {
		GMOrderInfo gOrderInfo = new GMOrderInfo();
		System.out.println("translateOrder");
		try {
			if(req.getOrder().contains("kick_machine")) {
				gOrderInfo.setType(GMOrderStatus.KICK_MACHINE);			
			}else if (req.getOrder().contains("kick_player")) {
				gOrderInfo.setType(GMOrderStatus.KICK_PLAYER);			
			}else if (req.getOrder().contains("set_vip")) {
				gOrderInfo.setType(GMOrderStatus.SET_VIP);	
			}else if (req.getOrder().contains("get_time")) {
				gOrderInfo.setType(GMOrderStatus.GET_TIME);	
			}else if (req.getOrder().contains("get_coin")) {
				gOrderInfo.setType(GMOrderStatus.GET_COIN);	
			}else {
				return null;
			}
			int mid =req.getMachineId();
			int num=0;
			if(gOrderInfo.getType()<3) {
				if(req.getOrder().contains("id"))
					mid= Integer.parseInt(req.getOrder().substring(req.getOrder().indexOf("id")+3));
			}else {
				if(req.getOrder().contains("id")) {
					mid= Integer.parseInt(req.getOrder().substring(req.getOrder().indexOf("id")+3));
				}
					num =Integer.parseInt(req.getOrder().substring(req.getOrder().lastIndexOf("=")+1));
			}
			gOrderInfo.setMid(mid);
			gOrderInfo.setNum(num);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return gOrderInfo;
	}
	//只有一个线程读写，不需要加锁
	public void addGM(String openId) {
			gmList.add(openId);
		
	}
	
	public void removeGM(String openId) {
			if (gmList.contains(openId)) {
				//System.out.println("remove gm player from list");
				for(int x =0;x<gmList.size();x++) {
					if(openId.equals(gmList.get(x))) {
						gmList.remove(x);
						return;
					}
				}
			}		
	}
	
	public boolean IsGM(String openId) {
		return gmList.contains(openId);
	}

}
